<template>
    <!-- v-if="isShowJSC || isShowJob" -->
    <view class="flex">
        <view v-if="isShowJSC" class="item" @click="toWorkReport">
            <image :src="dataPlanImgObj.JSCImg" mode="widthFix"></image>
        </view>
        <view v-if="isShowJob" class="item" @click="toPersonalStats">
            <image :src="dataPlanImgObj.JSCJob" mode="widthFix"></image>
        </view>
    </view>
</template>

<script>
import { getAppUrl } from '@/config/server'

export default {
    name: 'dataPanel',
    props: {
        menu: Object,
    },
    data() {
        return {
            dataPlanImgObj:{
                JSCImg: `${this.$baseIMGUrl}/static/assets/img/new05.png`,
                JSCJob: `${this.$baseIMGUrl}/static/assets/img/new06.png`
            }
        }
    },
    computed: {
        appGroups() {
            return this.$store.state.workbench.appList.filter(v => v.classifyName === '数据仓')
        },
        appUserInfo() {
            return uni.getStorageSync('appUserInfo') ?  JSON.parse(uni.getStorageSync('appUserInfo')) : {}
        },
        // 是否显示驾驶舱菜单
        isShowJSC() {
            return this.isExsistApp('sjc_gljsc')

        },
        // 是否显示工作统计
        isShowJob() {
            return this.isExsistApp('sjc_wdgztj')
        }
    },
    methods: {
        isExsistApp(appCode) {
            return 1
            if (!this.appGroups.length || !this.appGroups[0].apps || !this.appGroups[0].apps.length) {
                return false
            }
            const target = this.appGroups[0].apps.find(v => v.appCode === appCode)
            return !!target
        },
        toWorkReport() {
            this.openWebview('report')
        },
        toPersonalStats() {
            this.openWebview('personalStats')
        },
        getMenuInfo(routeUrl) {
            const apps = this.appGroups[0].apps || []
            if (routeUrl === 'report') {
                return apps.find(v => v.appCode === 'sjc_gljsc') // 管理驾驶舱
            } else {
                return apps.find(v => v.appCode === 'sjc_wdgztj') // 我的工作统计
            }
        },
        openWebview(routeUrl) {
            const menuInfo = this.getMenuInfo(routeUrl)
            if (!menuInfo) {
                // this.$toast('未找到对应应用信息')
                return
            }
            console.log(this.appUserInfo, 'aaa')

            const { memberId } = this.appUserInfo
            const { appCode, code, orgId, relationAppId, appServiceId } = menuInfo
            console.log(menuInfo, '112344')
            // debugger
            //   return;
            if (1) {
                // // h5 的处理
                // let localURL1 = getAppUrl(appCode, code, routeUrl, menuInfo)
                // console.log(localURL1, 'url111')
                let token1 = JSON.parse(uni.getStorageSync('refreshToken')).token
                // let url = `http:127.0.0.1:7088/roc-workorder-h5/#/report?wc=1&token=${token1}&orgId=${orgId}&appId=${relationAppId}`
                let url = `/roc-workorder-h5/#/${routeUrl}?orgId=${orgId}&typeapp=1&appServiceId=${appServiceId}&memberId=${memberId}&appId=${relationAppId}`;

                let token = uni.getStorageSync('XUserToken') || '';

                url = url + `&XUserToken=${token}`;

                uni.setStorageSync('appMenuParams', JSON.stringify({
                    appId: menuInfo.appId,
                    refDevopsAppId: menuInfo.refDevopsAppId
                }));

                // uni.navigateTo({
                //     url: `/pages/web-view/index?h5Url=${encodeURIComponent(url)}`
                // });
                console.log(url, 'url')
                // return 
                this.$Router.push({
                    name: 'MeWebView',
                    params: {
                        url,
                    }
                })
                return
            }
            let localURL = getAppUrl(appCode, code, routeUrl, menuInfo, this.appUserInfo)
            const token = uni.getStorageSync('XUserToken') || ''

            // 存储应用参数到小程序本地存储
            uni.setStorageSync('appMenuParams', JSON.stringify({
                appId: menuInfo.appId,
                refDevopsAppId: menuInfo.refDevopsAppId
            }))
            // localURL = '/roc-component-workbench/#/home/rentManagement'
            // 小程序环境处理（无需区分H5和Plus，统一用web-view）
            console.log(localURL, 'url')
            if (localURL) {
                // 拼接token参数
                localURL = '/' + localURL + (localURL.includes('?') ? '&' : '?') + `XUserToken=${token}`
                // console.log(localURL, 'url')
                // console.log(encodeURIComponent(localURL), 'url')
                this.$Router.push({
                    name: 'MeWebView',
                    params: {
                        url: localURL,
                    }
                })
            } else {
                // this.$toast('appCode不正确')
            }
        }
    }
}
</script>

<style lang="scss" scoped>
.flex {
    display: flex;
    color: #fff;

    .item {
        border-radius: 0.2rem;
        font-size: 0.32rem;
        flex: 1;

        image {
            width: 100%;
            height: 89rpx; // 小程序推荐使用rpx单位适配
        }

        .sub {
            line-height: 0.6rem;
        }

        &:first-of-type {
            margin-right: 10rpx;
        }

        &:last-of-type {
            margin-left: 10rpx;
        }

        .arrow {
            position: relative;
            display: inline-block;

            &:after {
                border: 2px solid #fff;
                border-bottom-width: 0;
                border-left-width: 0;
                content: " ";
                top: -0.1rem;
                right: -0.15rem;
                position: absolute;
                width: 5px;
                height: 5px;
                -webkit-transform: translateY(-50%) rotate(45deg);
                transform: translateY(-50%) rotate(45deg);
            }
        }
    }
}
</style>